const { Client, CommandInteraction, MessageEmbed } = require('discord.js');

module.exports = {
  name: 'interactionCreate',
  /**
   *
   * @param {CommandInteraction} interaction
   * @param {Client} client
   */

  async execute(interaction, client) {
    if (
      client.commands.permissions &&
      !interaction.member.permissions.has(client.commands.permissions)
    ) {
      return interaction.reply({
        content: `You do not have the required permission for this command: \`${interaction.commandName}\`.`,
        ephemeral: true,
      });
    }

    if (interaction.isCommand() || interaction.isContextMenu()) {
      const command = client.commands.get(interaction.commandName);
      if (!command)
        return (
          interaction.reply({
            embeds: [
              new MessageEmbed()
                .setColor('RED')
                .setDescription(
                  '⛔ An error occured while running this command.'
                ),
            ],
          }) && client.commands.delete(interaction.commandName)
        );

      command.execute(interaction, client);
    }
  },
};
